﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kooboo.Commerce.Payments.PayPal;
using Kooboo.Commerce.Contexts.Orders;

namespace Kooboo.Commerce.Payments.PayPal.Controllers
{

    [Authorize(Roles = "Administrator")]
    public class PayPalController : Controller
    {
        public ActionResult Complete(int id)
        {
            //    NVPAPICaller test = new NVPAPICaller();

            //    string retMsg = "";
            //    string token = "";
            //    string payerId = "";


            //    var settings = Services.FindSettings();
            //    test.SetCredentials(settings.APIUsername, settings.APIPassword, settings.APISignature);

            //    PaypalOrderTokenDataContext orderTokenDataContext = new PaypalOrderTokenDataContext();
            //    Payment_Paypal_OrderToken orderToken = orderTokenDataContext.Payment_Paypal_OrderToken.FirstOrDefault(i => i.OrderId == id);

            //    token = orderToken.Token;

            //    var host = new ServiceHost();

            //    IOrder order = host
            //        .QueryOrders()
            //        .Where(i => i.Id == id)
            //        .FirstOrDefault();
            //    NVPCodec decoder;
            //    bool ret = test.GetShippingDetails(token, ref payerId, out decoder, ref retMsg);
            //    if (ret)
            //    {
            //        order.FirstName = decoder["FIRSTNAME"];
            //        order.LastName = decoder["LASTNAME"];
            //       // order.ShippingName = decoder["SHIPTONAME"];
            //        order.Address1 = decoder["SHIPTOSTREET"];
            //        order.Address2 = decoder["SHIPTOSTREET2"];
            //        order.City = decoder["SHIPTOCITY"];
            //        order.State = decoder["SHIPTOSTATE"];
            //        order.Postcode = decoder["SHIPTOZIP"];
            //        host.Put(order);

            //        orderToken.PayerId = payerId;
            //        orderTokenDataContext.SubmitChanges();
            //        //Response.Write(shippingAddress);
            //    }
            //    else
            //    {
            //        Response.Redirect("APIError.aspx?" + retMsg);
            //    }


            //    string[] finalPaymentAmounts = order.Total.ToString().Split(new char[] { '.' });
            //    string finalPaymentAmount = finalPaymentAmounts[0] + "." + finalPaymentAmounts[1].Substring(0, 2);
            //    NVPCodec decoderR = new NVPCodec();

            //    ret = test.ConfirmPayment(finalPaymentAmount, token, payerId, ref decoderR, ref retMsg);
            //    if (ret) 
            //    {
            //       // order.OrderStatus = (int)OrderStatus.Complete;
            //        order.IsCompleted = true;
            //        host.Put(order);
            //        return this.RedirectToAction("Complete", "Order", new { id = id });
            //    }

            return View();
        }

        [HttpGet]
        public ActionResult APIError(string ErrorCode, string Desc, string Desc2)
        {
            if (string.IsNullOrEmpty(ErrorCode))
                TempData["ErrorCode"] = default(string);
            else
                TempData["ErrorCode"] = ErrorCode;

            if (string.IsNullOrEmpty(Desc))
                TempData["Desc"] = default(string);
            else
                TempData["Desc"] = Desc;

            if (string.IsNullOrEmpty(Desc2))
                TempData["Desc2"] = default(string);
            else
                TempData["Desc2"] = Desc2;
            return View();
        }

        public ActionResult Settings()
        {
            var settings = Services.FindSettings();
            return View(settings);
        }

        [HttpPost]
        public ActionResult Settings(Models.Settings settings, string next)
        {
            settings.APIPassword = "APIPassword";
            settings.APIUsername = "APIUsername";
            settings.APISignature = "APISignature";
            Services.Update(settings);

            var url = string.Empty;
            if (next == "prev")
            {
                url = this.Url.Action("Edit", "Payment", new { Name = "Paypal" });
            }
            else
            {
                url = this.Url.Action("Index", "Payment");
            }


            return Json(new { Data = url });
        }
    }
}
